﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using uMES.LeanManufacturing.DBUtility;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ReportBusiness;
using System.IO;

public partial class left : System.Web.UI.Page
{
    public string strSbuider = "";
    StringBuilder sb = new StringBuilder();
    uMESLoginBusiness login = new uMESLoginBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EmployeeInfoDTO dto = Session["employeeinfo"] as EmployeeInfoDTO;
            if (dto == null)
            {
                Response.Write("<script type='text/javascript'>alert('您当前未登录，请先登录！');</script>");
                Response.Write("<script type='text/javascript'>window.open('Login.aspx','_top');</script>");
                Response.Write("<script type='text/javascript'>window.open('Login.aspx','_parent');</script>");
                Response.Write("<script type='text/javascript'>window.parent.opener=null;window.top.close();</script>");
                Response.Write("<script type='text/javascript'>window.open('Login.aspx');</script>");
                return;
            }

            //获取角色信息
            DataTable dtRole = login.GetEmployeeRole(dto.Employeeid);

            //看板
            DataTable dtKanban = new DataTable();
            dtKanban.Columns.Add("Name");
            dtKanban.Columns.Add("Range");
            dtKanban.Columns.Add("PageName");

            //报表
            DataTable dtReport = new DataTable();
            dtReport.Columns.Add("Name");
            dtReport.Columns.Add("Range");
            dtReport.Columns.Add("PageName");

            //获取路径信息
            GetPathInfo(dtRole, ref dtKanban, ref dtReport);

            string strName = dto.RealmName;
            string blank = "_blank";

            string iconUrl = "TreeImages/chart.png";
            sb.Append("<script type='text/javascript'>");
            sb.Append("d = new dTree('d');");
            sb.Append("d.config.target = 'mainFrame';");
            sb.Append("d.config.folderLinks = false;");
            sb.Append("d.add(0, -1, '<B>报表系统</B>', '', 'MES决策支持系统');");

            //看板
            sb.Append("d.add(1, 0, '生产看板', '', '看板形式显示数据', '', '', '', true);");
            if (dtKanban.Rows.Count > 0)
            {
                for (int i = 0; i < dtKanban.Rows.Count; i++)
                {
                    if (dtKanban.Rows[i]["Range"].ToString() == "管理部")
                    {
                        sb.Append(string.Format("d.add(10{0},1,'{1}','{2}','{1}','{3}','{4}');", i, dtKanban.Rows[i]["name"].ToString(), "Kanban/" + dtKanban.Rows[i]["pagename"].ToString(), blank, iconUrl));
                    }
                    if (dtKanban.Rows[i]["Range"].ToString() == "车间")
                    {
                        sb.Append(string.Format("d.add(10{0},1,'{1}','{2}','{1}','{3}','{4}');", i, dtKanban.Rows[i]["name"].ToString(), "Kanban/" + dtKanban.Rows[i]["pagename"].ToString() + "?factoryid=" + dto.FactoryId, blank, iconUrl));
                    }
                    if (dtKanban.Rows[i]["Range"].ToString() == "班组")
                    {
                        sb.Append(string.Format("d.add(10{0},1,'{1}','{2}','{1}','{3}','{4}');", i, dtKanban.Rows[i]["name"].ToString(), "Kanban/" + dtKanban.Rows[i]["pagename"].ToString() + "?teamid=" + dto.TeamId, blank, iconUrl));
                    }
                }
            }

            //报表
            sb.Append("d.add(2, 0, '生产报表', '', '报表形式显示数据', '', '', '', true);");

            if (dtReport.Rows.Count > 0)
            {
                for (int i = 0; i < dtReport.Rows.Count; i++)
                {
                    sb.Append(string.Format("d.add(20{0},2,'{1}','{2}','{1}');", i, dtReport.Rows[i]["name"].ToString(), "Report/" + dtReport.Rows[i]["pagename"].ToString() + "?factoryid=" + dto.FactoryId));
                }
            }

            sb.Append("document.write(d);");
            sb.Append("</script>");
            strSbuider = sb.ToString();
        }
    }


    //获取路径信息
    protected void GetPathInfo(DataTable dtRole, ref DataTable dtKanban, ref DataTable dtReport)
    {
        string strExcelName = ConfigurationManager.AppSettings["ExcelName"];
        //String strDocPath = Server.MapPath(Request.ApplicationPath) + "\\" + strExcelName; // ConfigurationManager.AppSettings("ImportExcelPatch")
        string type = "";
        if (!string.IsNullOrEmpty(strExcelName))
        {
            type = strExcelName.Substring(strExcelName.LastIndexOf(".") + 1);
        }
        string strconn = "";

        if (type.ToLower() == "xls")
        {
            strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelName + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
        }
        else if (type.ToLower() == "xlsx")
        {
            strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelName + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
        }
        else
        {
            return;
        }
        DataTable dt = new DataTable();
        using (var odbcConnection = new System.Data.OleDb.OleDbConnection(strconn))
        {
            try
            {
                odbcConnection.Open();
                var strsql = "select * from [Sheet1$]";
                var odbcDataAdapater = new System.Data.OleDb.OleDbDataAdapter(strsql, odbcConnection);
                odbcDataAdapater.Fill(dt);
                odbcConnection.Close();
            }
            catch (Exception ex)
            {

            }
        }

        if (dt.Rows.Count > 0)
        {
            //看板
            DataRow[] drAll = dt.Select("type = '看板'");
            if (drAll.Length > 0)
            {
                foreach (DataRow dr in drAll)
                {
                    if (!string.IsNullOrEmpty(dr["pageName"].ToString()))
                    {
                        if (dtKanban.Select("pageName='" + dr["pageName"].ToString() + "'").Length == 0)
                        {
                            dtKanban.Rows.Add();
                            dtKanban.Rows[dtKanban.Rows.Count - 1]["Name"] = dr["Name"].ToString();
                            dtKanban.Rows[dtKanban.Rows.Count - 1]["Range"] = dr["Range"].ToString();
                            dtKanban.Rows[dtKanban.Rows.Count - 1]["pageName"] = dr["pageName"].ToString();
                        }
                    }
                }
            }

            //报表
            if (dtRole.Rows.Count > 0)
            {
                if (drAll.Length > 0)
                {
                    //角色信息
                    foreach (DataRow dr1 in dtRole.Rows)
                    {
                        drAll = dt.Select("type = '报表'");
                        if (drAll.Length > 0)
                        {
                            foreach (DataRow dr in drAll)
                            {
                                if (!string.IsNullOrEmpty(dr["pageName"].ToString()))
                                {
                                    if (dr["rolename"].ToString().Contains(dr1["rolename"].ToString()))
                                    {
                                        if (dtReport.Select("pageName='" + dr["pageName"].ToString() + "'").Length == 0)
                                        {
                                            dtReport.Rows.Add();
                                            dtReport.Rows[dtReport.Rows.Count - 1]["Name"] = dr["Name"].ToString();
                                            dtReport.Rows[dtReport.Rows.Count - 1]["Range"] = dr["Range"].ToString();
                                            dtReport.Rows[dtReport.Rows.Count - 1]["pageName"] = dr["pageName"].ToString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
